import 'dart:developer';

import 'package:manager/configs/image_path.dart';
import 'package:common_ui/components/common_layout.dart';
import 'package:common_ui/components/common_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:get/get.dart';
import 'package:manager/base/base_controller.dart';
import 'package:manager/configs/theme_data.dart';
import 'package:manager/configs/widget_ids.dart';
import 'package:manager/res/color.dart';
import '../exts/app_ext.dart';

///带有title,没有下拉刷新的抽象页面类
abstract class CommonPage<T extends BaseController> extends GetView<T> {
  @override
  Widget build(BuildContext context) {
    return ToolbarPage(
      getTitle(),
      Column(
        children: [
          getTopWidget(),
          GetBuilder(
            id: EMOTION_STATUS,
            init: controller,
            builder: (logic) {
              return Expanded(
                  child: GestureDetector(
                onTap: () => hideSoftKeyboard(),
                child: EmotionWidget(
                  getChild(),
                  emptyWidget: emptyView(),
                  controller.status,
                  retry: () => controller.retry(),
                ),
              ));
            },
          ),
          Row(
            children: getBottomButton(),
          )
        ],
      ),
      bgColor: getBgColor() ?? ColorPool.activity_bg,
      actions: getAction(),
      resizeBottom: getResizeBottom(),
    );
  }

  Color? getBgColor() {
    return ColorPool.activity_bg;
  }

  emptyView() {
    return Container(
      padding: const EdgeInsets.only(left: 24, bottom: 24),
      margin: const EdgeInsets.only(bottom: 24),
      alignment: Alignment.center,
      child: Image.asset(
        '${ImagePath.IMAGE_PATH}no_data.png',
        fit: BoxFit.contain,
        width: 260,
      ),
    );
  }

  ///页面标题
  String getTitle();

  ///页面内容widget
  Widget getChild();

  List<Widget> getAction() {
    return [];
  }

  bool? getResizeBottom() {
    return false;
  }

  ///底部按钮列表
  List<Widget> getBottomButton() {
    return [];
  }

  ///顶部组件
  Widget getTopWidget() {
    return Container();
  }
}

///没有title
abstract class CommonPageNoTitle<T extends BaseController> extends GetView<T> {
  @override
  Widget build(BuildContext context) {
    return Container(
        width: double.infinity,
        height: double.infinity,
        color: getBgColor(),
        child: GetBuilder(
          id: EMOTION_STATUS,
          init: controller,
          builder: (logic) {
            return Column(
              children: [
                getTopWidget(),
                Expanded(
                    child: EmotionWidget(
                  getChild(),
                  controller.status,
                  retry: () => controller.retry(),
                )),
                Row(
                  children: getBottomButton(),
                )
              ],
            );
          },
        ));
  }

  ///页面内容widget
  Widget getChild();

  ///底部按钮列表
  List<Widget> getBottomButton() {
    return [];
  }

  ///顶部组件
  Widget getTopWidget() {
    return Container();
  }

  Color getBgColor() {
    return ColorPool.activity_bg;
  }
}

///带有搜索框的页面,没有下拉刷新的抽象页面类
abstract class SearchPage<T extends BaseController> extends GetView<T> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: _searchBar(),
        actions: getAction(),
        elevation: 0,
      ),
      body: GetBuilder(
        id: EMOTION_STATUS,
        init: controller,
        builder: (logic) {
          return EmotionWidget(
            getChild(),
            emptyWidget: emptyView(),
            controller.status,
            retry: () => controller.retry(),
          );
        },
      ),
      backgroundColor: ColorPool.activity_bg,
    );
  }

  ///顶部的搜索框
  Widget _searchBar() {
    return Container(
      padding: const EdgeInsets.only(left: 8),
      width: double.infinity,
      height: 35,
      alignment: Alignment.center,
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(20)),
      child: TextField(
        style: Styles.textMedium,
        onChanged: (value) => searchKey(value),
        onSubmitted: (v) => submit(),
        textInputAction: TextInputAction.search,
        maxLines: 1,
        decoration: InputDecoration(
            contentPadding: const EdgeInsets.only(left: -8),
            isCollapsed: true,
            icon: Icon(
              CupertinoIcons.search,
              size: 24,
              color: Colors.grey[400],
            ),
            hintText: getHint(),
            hintStyle:
                const TextStyle(fontSize: 12, color: ColorPool.text_grey),
            border: InputBorder.none),
      ),
    );
  }

  void submit() {}

  ///页面内容widget
  Widget getChild();

  List<Widget> getAction() {
    return [];
  }

  void searchKey(String? key) {}

  ///搜索框的提示信息
  String getHint();

  emptyView() {
    return Container(
      padding: const EdgeInsets.only(left: 24, bottom: 24),
      margin: const EdgeInsets.only(bottom: 24),
      alignment: Alignment.center,
      child: Image.asset(
        '${ImagePath.IMAGE_PATH}no_data.png',
        fit: BoxFit.contain,
        width: 260,
      ),
    );
  }
}

///带有title,没有下拉刷新的抽象页面类
abstract class TabViewPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => TabViewState();

  ///页面标题
  Widget getTitle();

  ///toolbar 右侧操作按钮
  List<Widget> getAction() {
    return [];
  }

  Widget? floatingButton() {
    return null;
  }

  ///指示线颜色
  Color indicatorColor() {
    return ColorPool.theme_color;
  }

  ///字体颜色
  Color labelColor() {
    return Get.theme.primaryColor;
  }

  ///tab背景颜色
  Color tabBgColor() {
    return Colors.white;
  }

  ///tab分割线颜色
  Color lineColor() {
    return Colors.white;
  }

  ///字体颜色
  Color unselectedLabelColor() {
    return Get.theme.hintColor;
  }

  ///在tab和toolbar中间的组件
  Widget topView() {
    return Container();
  }

  Widget bottomView() {
    return Container();
  }

  ///遮盖物
  Widget overlayView() {
    return Container();
  }

  ///tab的标题名称
  List<String> tabTitle();

  List<Widget> tabViews();

  int initPos() {
    return 0;
  }
}

class TabViewState extends State<TabViewPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    log('initState----${this}');
    _tabController = TabController(
        initialIndex: widget.initPos(),
        length: widget.tabTitle().length,
        vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: widget.getTitle(),
        centerTitle: true,
        elevation: 0,
        actions: widget.getAction(),
      ),
      resizeToAvoidBottomInset: false,
      body: Stack(
        children: [
          Column(
            children: [
              widget.topView(),
              Container(
                height: 45,
                decoration: BoxDecoration(
                    color: widget.tabBgColor(),
                    border:
                        Border(bottom: BorderSide(color: widget.lineColor()))),
                child: TabBar(
                  controller: _tabController,
                  tabs: widget
                      .tabTitle()
                      .map((e) => SingleText(
                            e,
                            height: 45,
                            wide: e.length * 16,
                            alignment: Alignment.center,
                          ))
                      .toList(),
                  indicatorSize: TabBarIndicatorSize.label,
                  indicatorWeight: 2,
                  indicatorColor: widget.indicatorColor(),
                  labelColor: widget.labelColor(),
                  unselectedLabelColor: widget.unselectedLabelColor(),
                  onTap: (pos) {},
                ),
              ),
              Expanded(
                  child: TabBarView(
                controller: _tabController,
                children: widget.tabViews(),
              )),
              widget.bottomView()
            ],
          ),
          widget.overlayView()
        ],
      ),
      backgroundColor: ColorPool.activity_bg,
      floatingActionButton: widget.floatingButton(),
    );
  }
}
